﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;


namespace plagiat_tp
{
    class Shingle
    {
        const int ShLength = 4; 
        string[] ShnglArray;

        private string[] Shngl(string text)
        {
            if (text != null)
            {
                ShnglArray = new string[1];
                string[] WordArray = text.Split(' ');

                for (int i = 0; i < WordArray.Length; i++)
                {
                    if (i < WordArray.Length - ShLength)
                    {
                        for (int j = 0; j < ShLength; j++)
                            ShnglArray[i] += WordArray[i + j] + ' ';
                        Array.Resize(ref ShnglArray, ShnglArray.Length + 1);
                    }
                }
                Array.Resize(ref ShnglArray, ShnglArray.Length - 1);

            }
            return ShnglArray;
        }

        public string[] getShingleArr()
        {
            return ShnglArray;
        }

        private string[] ControlSumm(string[] sh)
        {
            string[] csArr = new string[sh.Length];
            for (int i = 0; i < csArr.Length-1; i++)
            {
               
                MD5 md = MD5.Create();
                byte[] data = md.ComputeHash(Encoding.Default.GetBytes(sh[i]));
                StringBuilder sBuilder = new StringBuilder();

               
                for (int j = 0; j < data.Length; j++)
                {
                    sBuilder.Append(data[j].ToString("x2"));
                }
                csArr[i] = sBuilder.ToString();
            }
            return csArr;
        }
       
       

        public string[] GetControlSumms(string text)
        {
            return  ControlSumm(Shngl(text));

            
        }
        

    }
}
